package com.gitee.neuray.security.par.ParExamPlan.controller;

import com.alibaba.fastjson.JSONObject;
import com.gitee.neuray.security.common.msg.ListRestResponse;
import com.gitee.neuray.security.common.msg.ObjectRestResponse;
import com.gitee.neuray.security.common.msg.TableResultResponse;
import com.gitee.neuray.security.common.util.Query;
import com.gitee.neuray.security.par.ParExamCate.biz.ParExamCateBiz;
import com.gitee.neuray.security.par.ParExamCate.entity.ParExamCateEntity;
import com.gitee.neuray.security.par.ParExamItem.biz.ParExamItemBiz;
import com.gitee.neuray.security.par.ParExamItem.entity.ParExamItemEntity;
import com.gitee.neuray.security.par.ParExamPlan.entity.ParExamPlanEntity;
import com.gitee.neuray.security.par.ParExamPlan.biz.ParExamPlanBiz;
import com.gitee.neuray.security.par.basicData.entity.ParBasicDataEntity;
import org.springframework.web.bind.annotation.*;
import com.gitee.neuray.security.common.rest.BaseController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 考评计划表
 *
 * @author dev
 * @email ${email}
 * @date 2020-08-27 10:28:11
 */
@RestController
@RequestMapping("/examPlan")
public class ParExamPlanController extends BaseController<ParExamPlanBiz, ParExamPlanEntity>{

    @Resource
    private ParExamPlanBiz parExamPlanBiz;

    @Resource
    private ParExamCateBiz parExamCateBiz;

    @Resource
    private ParExamItemBiz parExamItemBiz;

    /**
     * 查询计划表（部门）
     * @param params
     * @return
     */
    @RequestMapping(value = "/queryPlan", method = RequestMethod.GET)
    public TableResultResponse<ParExamPlanEntity> queryPlan(@RequestParam Map<String, Object> params){
        params.put("scbj","0");
        Query query = new Query(params);
        ParExamPlanEntity parExamPlanEntity = JSONObject.parseObject(JSONObject.toJSONString(params),ParExamPlanEntity.class);
        //return parExamPlanBiz.selectByQuery(query);
        return parExamPlanBiz.queryPlan(query,parExamPlanEntity);
    }

    /**
     * 查询计划表（个人）
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryPlanPersonnel", method = RequestMethod.GET)
    public TableResultResponse<ParExamPlanEntity> queryPlanPersonnel(@RequestParam Map<String, Object> params,HttpServletRequest request){
        String kpr = request.getHeader("orgCode");
        params.put("scbj","0");
        params.put("kpr",kpr);
        Query query = new Query(params);
        ParExamPlanEntity parExamPlanEntity = JSONObject.parseObject(JSONObject.toJSONString(params),ParExamPlanEntity.class);
        //return parExamPlanBiz.selectByQuery(query);
        return parExamPlanBiz.queryPlan(query,parExamPlanEntity);
    }

    /**
     * 新增计划（部门）
     * @param parExamPlanEntity
     * @return
     */
    @RequestMapping(value = "/insertPlan", method = RequestMethod.POST)
    public ObjectRestResponse<ParExamPlanEntity> insertPlan(@RequestBody ParExamPlanEntity parExamPlanEntity){
        parExamPlanBiz.insertPlan(parExamPlanEntity,"org",null);
        return new ObjectRestResponse<ParExamPlanEntity>().rel(true);
    }

    /**
     * 新增计划（部门）
     * @param parExamPlanEntity
     * @param request
     * @return
     */
    @RequestMapping(value = "/insertPlanPersonnel", method = RequestMethod.POST)
    public ObjectRestResponse<ParExamPlanEntity> insertPlanPersonnel(@RequestBody ParExamPlanEntity parExamPlanEntity, HttpServletRequest request){
        parExamPlanBiz.insertPlan(parExamPlanEntity,"personnel",request.getHeader("orgCode"));
        return new ObjectRestResponse<ParExamPlanEntity>().rel(true);
    }

    /**
     * 查询计划项表
     * @param params
     * @return
     */
    @RequestMapping(value = "/queryCate", method = RequestMethod.GET)
    public ListRestResponse<List<ParExamCateEntity>> queryCate(@RequestParam Map<String,Object> params){
        Query query = new Query(params);
        ParExamCateEntity parExamCateEntity = JSONObject.parseObject(JSONObject.toJSONString(params),ParExamCateEntity.class);
        return parExamCateBiz.queryCate(query,parExamCateEntity);
    }
    /**
     * 查询计划项表明细（部门）
     * @param params
     * @return
     */
    @RequestMapping(value = "/queryCateItem", method = RequestMethod.GET)
    public ListRestResponse<List<ParBasicDataEntity>> queryCateItem(@RequestParam Map<String,Object> params){
        Query query = new Query(params);
        ParBasicDataEntity parBasicDataEntity = JSONObject.parseObject(JSONObject.toJSONString(params),ParBasicDataEntity.class);
        return parExamCateBiz.queryCateItem(query,parBasicDataEntity,"org",null);
    }
    /**
     * 查询计划项表明细（个人）
     * @param params
     * @return
     */
    @RequestMapping(value = "/queryCateItemPersonnel", method = RequestMethod.GET)
    public ListRestResponse<List<ParBasicDataEntity>> queryCateItemPersonnel(@RequestParam Map<String,Object> params,HttpServletRequest request){
        Query query = new Query(params);
        String deptCode = request.getHeader("orgCode");
        ParBasicDataEntity parBasicDataEntity = JSONObject.parseObject(JSONObject.toJSONString(params),ParBasicDataEntity.class);
        return parExamCateBiz.queryCateItem(query,parBasicDataEntity,"personnel",deptCode);
    }
    /**
     * 新增计划项表明细（部门）
     * @param param
     * @return
     */
    @RequestMapping(value = "/insertItem", method = RequestMethod.POST)
    public ObjectRestResponse<ParExamItemEntity> insertItem(@RequestBody Map param){

        List<ParExamItemEntity> parExamItemEntity = JSONObject.parseArray(JSONObject.toJSONString(param.get("list")),ParExamItemEntity.class);
        String kpxmflid = param.get("kpxmflid").toString();
        ParExamItemEntity deleteItem = new ParExamItemEntity();
        deleteItem.setKpxmflid(kpxmflid);
        parExamItemBiz.delete(deleteItem);
        for(ParExamItemEntity itemEntity : parExamItemEntity){
            parExamItemBiz.insertSelective(itemEntity);
        }
        return new ObjectRestResponse<ParExamPlanEntity>().rel(true);
    }

    /**
     * 查询考评周期下拉框
     * @return
     */
    @RequestMapping(value = "/queryPeriod", method = RequestMethod.GET)
    public ListRestResponse<List<Map>> queryPeriod(@RequestParam Map params){
        List<Map> ret = parExamPlanBiz.queryPeriod(params);
        return new ListRestResponse("", ret.size(), ret);
    }
}
